package org.jiangnantaste.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;

public class ImageUtil {
    private static File file = null;

    public static FileInputStream getImageByte(String infile) {
        FileInputStream imageByte = null;
        file = new File(infile);
        try {
            imageByte = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return imageByte;
    }    

    public static byte[] getImageBytes(InputStream in) {
        try {
        	ByteBuffer nbf = ByteBuffer.allocate(1024*1024);
            byte[] array = new byte[1024];
            int length = 0;
            int totallength = 0;
            while ((length = in.read(array)) != -1) {
            	nbf.put(array, 0, length);
            }
            byte[] content = nbf.array();
            return content;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    } 

    public static void readBlob(InputStream inputStream, String path) {
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(path);
            byte[] buffer = new byte[1024];
            int len = 0;
            while ((len = inputStream.read(buffer)) != -1) {
                fileOutputStream.write(buffer, 0, len);
            }
            inputStream.close();
            fileOutputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}